<?php

namespace pays\Gateways\alipay;

class Alipay {

    protected $config;

    public function __construct($config) {
        $this->config = $config;
    }

    //扫码支付
    public function qrcode($order = array()) {
        if (!is_array($order)) {
            die("请传入数组参数!");
        }
        if (!$this->check_params($order, ["total"])) {
            die("请传入付款金额!");
        }
        if (!$this->check_params($order, ["ordersn"])) {
            die("请传入订单编号!");
        }
        if (!$this->check_params($order, ["subject"])) {
            die("请传入付款描述参数!");
        }
        if (!$this->check_params($order, ["notify_url"])) {
            die("请传入异步回调地址参数!");
        }
        if (!$this->check_params($order, ["return_url"])) {
            die("请传入同步回调地址参数!");
        }
        require_once dirname(__FILE__) . '/f2fpay/model/builder/AlipayTradePrecreateContentBuilder.php';
        require_once dirname(__FILE__) . '/f2fpay/service/AlipayTradeService.php';
        $config = $this->config;
        $config["notify_url"] = $order["notify_url"];
        $config["return_url"] = $order["return_url"];
        // 创建请求builder，设置请求参数
        $qrPayRequestBuilder = new \AlipayTradePrecreateContentBuilder();
        $qrPayRequestBuilder->setOutTradeNo($order["ordersn"]);
        $qrPayRequestBuilder->setTotalAmount($order["total"]);
        $qrPayRequestBuilder->setSubject($order["subject"]);
        $qrPayRequestBuilder->setBody($order["subject"]);
        // 调用qrPay方法获取当面付应答
        $qrPay = new \AlipayTradeService($config);
        $qrPayResult = $qrPay->qrPay($qrPayRequestBuilder);
        switch ($qrPayResult->getTradeStatus()) {
            case "SUCCESS":
                $return_str = "支付宝创建订单二维码成功.";
                $response = $qrPayResult->getResponse();
                break;
            case "FAILED":
                $return_str = "支付宝创建订单二维码失败!!!";
                if (!empty($qrPayResult->getResponse())) {
                    $response = $qrPayResult->getResponse();
                }
                break;
            case "UNKNOWN":
                $return_str = "系统异常，状态未知!!!";
                if (!empty($qrPayResult->getResponse())) {
                    $response = $qrPayResult->getResponse();
                }
                break;
            default:
                $return_str = "不支持的返回状态，创建订单二维码返回异常!!!";
                break;
        }
        $result = array(
            "msg" => $return_str,
            "data" => $response
        );
        return $result;
    }

    //手机支付
    public function h5($order = array()) {
        if (!is_array($order)) {
            die("请传入数组参数!");
        }
        if (!$this->check_params($order, ["total"])) {
            die("请传入付款金额!");
        }
        if (!$this->check_params($order, ["ordersn"])) {
            die("请传入订单编号!");
        }
        if (!$this->check_params($order, ["subject"])) {
            die("请传入付款描述参数!");
        }
        if (!$this->check_params($order, ["notify_url"])) {
            die("请传入异步回调地址参数!");
        }
        if (!$this->check_params($order, ["return_url"])) {
            die("请传入同步回调地址参数!");
        }
        require_once dirname(__FILE__) . '/wappay/service/AlipayTradeService.php';
        require_once dirname(__FILE__) . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php';

        $config = $this->config;
        $config["notify_url"] = $order["notify_url"];
        $config["return_url"] = $order["return_url"];
        // 创建请求builder，设置请求参数
        //超时时间
        $timeout_express = "1m";
        $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
        $payRequestBuilder->setBody($order["subject"]);
        $payRequestBuilder->setSubject($order["subject"]);
        $payRequestBuilder->setOutTradeNo($order["ordersn"]);
        $payRequestBuilder->setTotalAmount($order["total"]);
        $payRequestBuilder->setTimeExpress($timeout_express);
        $payResponse = new \AlipayTradeService($config);
        $result = $payResponse->wapPay($payRequestBuilder, $config['return_url'], $config['notify_url']);
        return "";
    }
    //订单查询
    public function query($input = array()) {
        if (!is_array($input)) {
            die("请传入数组参数!");
        }
        if (!$this->check_params($input, ["order_sn"])) {
            die("请传入支付单号!");
        }
        require_once dirname(__FILE__) . '/wappay/buildermodel/AlipayTradeQueryContentBuilder.php';
        require_once dirname(__FILE__) . '/wappay/service/AlipayTradeService.php';
        $RequestBuilder = new \AlipayTradeQueryContentBuilder();
        $RequestBuilder->setOutTradeNo($input["order_sn"]);
        $Response = new \AlipayTradeService($config);
        $result=$Response->Query($RequestBuilder);
        //dump($result);
        return $result;
    }

    //订单退款
    public function refund($input = array()) {
        if (!is_array($input)) {
            die("请传入数组参数!");
        }
        if (!$this->check_params($input, ["order_sn"])) {
            die("请传入订单编号!");
        }
        if (!$this->check_params($input, ["total_fee"])) {
            die("请传入订单支付金额!");
        }
        if (!$this->check_params($input, ["refund_reason"])) {
            die("请传入退款原因!");
        }
        require_once dirname(__FILE__) . '/wappay/buildermodel/AlipayTradeRefundContentBuilder.php';
        require_once dirname(__FILE__) . '/wappay/service/AlipayTradeService.php';
        //商户订单号和支付宝交易号不能同时为空。 trade_no、  out_trade_no如果同时存在优先取trade_no
        //商户订单号，和支付宝交易号二选一
        $out_trade_no = trim($input['order_sn']);
        //支付宝交易号，和商户订单号二选一
        //$trade_no = trim($input['request_no']);
        //退款金额，不能大于订单总金额
        $refund_amount = trim($input['total_fee']);
        //退款的原因说明
        $refund_reason = trim($input['refund_reason']);
        //标识一次退款请求，同一笔交易多次退款需要保证唯一，如需部分退款，则此参数必传。
        //$out_request_no = trim($input['request_no']);
        $RequestBuilder = new \AlipayTradeRefundContentBuilder();
        //$RequestBuilder->setTradeNo($trade_no);
        $RequestBuilder->setOutTradeNo($out_trade_no);
        $RequestBuilder->setRefundAmount($refund_amount);
        $RequestBuilder->setRefundReason($refund_reason);
        //$RequestBuilder->setOutRequestNo($out_request_no);
        $Response = new \AlipayTradeService($config);
        $result = $Response->Refund($RequestBuilder);
        return $result;
    }

    protected function check_params($data, $c = array()) {
        if (empty($c)) {
            return $data;
        }
        foreach ($c as $k => $v) {
            if (array_key_exists($v, $data) == false) {
                return false;
            }
        }
        return $data;
    }

}
